<?php
namespace Common\Model;


class StatisticalModel extends BaseModel
{

    protected $_validate = [
        ['name', '', '该名称已存在', self::EXISTS_VALIDATE , 'unique'],
    ];

    protected $_auto = [
        ['update_time', 'time', self::MODEL_BOTH, 'function'],
    ];

    /**
     * 添加统计点击量
     *
     * @return void
     */
    public function addCount($name)
    {
        $record = D('Statistical')->getByName($name);
        if ($record) {
            D('Statistical')->where(['name' => $name])->setInc('val');
            D('Statistical')->where(['name' => $name])->setField('update_time', time());
        } else {
            $data = [
                'name' => $name,
                'val' => 1,
            ];
            $mStatistical = D('Statistical');
            if ($mStatistical->create($data)) {
                $mStatistical->add();
            }
        }
    }
}
